package com.hnxxxy.sysmgr.controller;

import com.github.pagehelper.PageHelper;
import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.annotation.LogAnno;
import com.hnxxxy.common.tools.Help;
import com.hnxxxy.common.tools.TableObj;
import com.hnxxxy.sysmgr.common.Constant;

import com.hnxxxy.sysmgr.pojo.Role;
import com.hnxxxy.sysmgr.service.RoleService;
import com.hnxxxy.sysmgr.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author Json
 * @date 2020-26-8:16
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     *查询所有角色
     * @param roleVo
     * @return
     */
    @RequestMapping("loadAllRole")
    @LogAnno(description = "查询所有角色")
    public TableObj queryAllStaffForList(TableObj obj, RoleVo roleVo){
        PageHelper.startPage(obj.getCurr(), obj.getNums());
        List<Role> list = roleService.queryAllRole(roleVo);
        return Help.getPageTableRtn(obj,list);
    }
    /**
     * 添加角色
     */
    @RequestMapping("addRole")
    @LogAnno(description = "添加角色",descriptiontype = "addRole")
    public AjaxRtnEntity addRole(RoleVo roleVo) {
        roleVo.setCreatetime(new Date());
        roleVo.setState(Constant.AVAILABLE_FALSE);
        return new AjaxRtnEntity(this.roleService.addRole(roleVo),"添加成功！",null);
    }
    /**
     * 修改角色
     */
    @RequestMapping("updateRole")
    @LogAnno(description = "更新角色",descriptiontype = "updateRole")
    public AjaxRtnEntity updateRole(RoleVo roleVo) {
        return new AjaxRtnEntity(this.roleService.updateRole(roleVo),"更新成功！",null);
    }

    /**
     * 删除角色
     */
    @RequestMapping("deleteRole")
    @LogAnno(description = "删除角色",descriptiontype = "deleteRole")
    public AjaxRtnEntity deleteRole(Integer id) {
        return new AjaxRtnEntity(this.roleService.deleteRole(id),"删除成功！",null);
    }

    /**
     * 批量删除角色
     */
    @RequestMapping("batchDeleterole")
    @LogAnno(description = "批量删除角色",descriptiontype = "batchDeleterole")
    public AjaxRtnEntity deleteBatchRoles(RoleVo roleVo) {
        return new AjaxRtnEntity(this.roleService.batchDeleterole(roleVo.getIds()),"删除成功！",null);
    }


    /**
     * 加载角色管理分配菜单的json
     */
    @RequestMapping("initRoleMenuTreeJson")
    public TableObj initRoleMenuTreeJson(Integer roleid) {
        return this.roleService.initRoleMenuTreeJson(roleid);
    }

    /**
     * 保存角色和菜单的关系
     * @param roleVo
     * @return
     */
    @RequestMapping("saveRoleToken")
    @LogAnno(description = "保存角色和菜单的关系",descriptiontype = "saveRoleToken")
    public AjaxRtnEntity saveRoleToken(RoleVo roleVo){
        return new AjaxRtnEntity(this.roleService.saveRoleToken(roleVo),"分配成功！",null);
    }
}
